
-module( logout ).

-author( "chenyiming" ).

-import(proplists, [get_value/3]).

-export([ handle/1 ]).

-include("emqttd.hrl").


handle( Params ) ->

    try
        UserId = get_value(<<"uid">>,   Params, undefined),
 
        Im  = util:get_value_cache( ham_server, emqttd, 'im_server@127.0.0.1' ),

        case UserId of
            undefined ->
                throw( {uid_undefined, Params} );
            _NotUndefined ->
            ok
        end,
    
        case rpc:call(Im, mnesia, dirty_read,  [ user_info, UserId ] ) of
            [] ->
               ok;
            [ OldUserInfo ] ->
                NewUserInfo = OldUserInfo#user_info{ platform_type = 1},
                ok = rpc:call(Im, mnesia, dirty_write, [ user_info, NewUserInfo  ] )
         end,
  
        case emysql:execute( im_db, "update user_info set pltype = '1', ios_token = 'android' where user_id = ? ", [ UserId ] ) of
                {ok_packet, _C, _F, _VL, _E, _L, _L1} ->
                    ok;
                _OthersError ->
                    throw( {mysql_error, _OthersError} )
                end,
        {ok, [{<<"code">>,0}]}
    catch
        M:R ->
            lager:error("register error: ~p ~p ~p~n", [M,R,erlang:get_stacktrace()]),
            {error, [{<<"code">>,5}]}
    end.




